//
//  IWNewfeatureViewController.m
//  ItcastWeibo
//
//  Created by apple on 14-5-6.
//  Copyright (c) 2014年 itcast. All rights reserved.
//

#import "NewfeatureViewController.h"
#import "PPTabBarController.h"
#import "LoginViewController.h"
#import "IWNavigationController.h"
#import "EGOCache.h"
#import "Advertise.h"
#import "UIImageView+WebCache.h"
#define Cache_AdvertiseKey @"AdvertiseCache"
#define CacheTime 24*60*60
#define padding 10
@interface NewfeatureViewController () <UIScrollViewDelegate>
@property (nonatomic, weak) UIPageControl *pageControl;
/**
 *  本地图片数组
 */
@property (nonatomic,strong)NSArray *localPictures;
/**
 *  缓存的图片数组
 */
@property (nonatomic,strong)NSArray *cachePictures;
/**
 *  广告图片的个数
 */
@property (nonatomic,assign)int advertiseCount;
@end

@implementation NewfeatureViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // 1.添加UISrollView
    [self setupScrollView];
    
    // 2.添加pageControl
    [self setupPageControl];
}
- (int)advertiseCount{
    if ([[EGOCache globalCache] hasCacheForKey:Cache_AdvertiseKey])
        return (int)self.cachePictures.count;
    else
        return (int)self.localPictures.count;
}
/**
 *  缓存数组
 */
- (NSArray *)cachePictures{
    if (![[EGOCache globalCache] hasCacheForKey:Cache_AdvertiseKey]) {
        _cachePictures = nil;
    }else if (_cachePictures==nil){
        _cachePictures = (NSArray *)[[EGOCache globalCache] objectForKey:Cache_AdvertiseKey];
    }
    return _cachePictures;
}
/**
 *  本地图片数组
 */
- (NSArray *)localPictures{
    if (_localPictures==nil) {
        _localPictures = @[@"new_feature_1",@"new_feature_2"];
    }
    return _localPictures;
}
/**
 *  添加pageControl
 */
- (void)setupPageControl
{
    // 1.添加
    UIPageControl *pageControl = [[UIPageControl alloc] init];
    pageControl.numberOfPages = self.advertiseCount;
    CGFloat centerX = self.view.frame.size.width * 0.5;
    CGFloat centerY = self.view.frame.size.height - 30;
    pageControl.center = CGPointMake(centerX, centerY);
    pageControl.bounds = CGRectMake(0, 0, 100, 30);
    pageControl.userInteractionEnabled = NO;
    [self.view addSubview:pageControl];
    self.pageControl = pageControl;
    
    // 2.设置圆点的颜色
    pageControl.currentPageIndicatorTintColor = APP_THEME_COLOR;
    pageControl.pageIndicatorTintColor = APP_BACK_COLOR;
}

/**
 *  添加UISrollView
 */
- (void)setupScrollView
{
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.frame = self.view.bounds;
    scrollView.delegate = self;
    [self.view addSubview:scrollView];
    
    // 2.添加图片
    CGFloat imageW = scrollView.frame.size.width;
    CGFloat imageH = scrollView.frame.size.height;
    for (int index = 0; index<self.advertiseCount; index++) {
        UIImageView *imageView = [[UIImageView alloc] init];
        // 设置图片
        if([[EGOCache globalCache] hasCacheForKey:Cache_AdvertiseKey]){
            imageView.contentMode = UIViewContentModeScaleAspectFit;
            imageView.image = [[EGOCache globalCache] imageForKey:[NSString stringWithFormat:@"imageCache%d",index+1]];
        }else{
            NSString *name = nil;
            name = self.localPictures[index];
            imageView.image = [UIImage imageWithName:name];
        }
        // 设置frame
        CGFloat imageX = index * imageW;
        imageView.frame = CGRectMake(imageX, 0, imageW, imageH);
        
        [scrollView addSubview:imageView];

        //添加跳过按钮
        [self setupSkipImageView:imageView];

    }
    
    // 3.设置滚动的内容尺寸
    scrollView.contentSize = CGSizeMake(imageW * self.advertiseCount, 0);
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.pagingEnabled = YES;
    scrollView.bounces = NO;
}

/**
 *  添加内容到最后一个图片
 */
- (void)setupSkipImageView:(UIImageView *)imageView
{
    // 0.让imageView能跟用户交互
    imageView.userInteractionEnabled = YES;
    
    // 1.添加开始按钮
    UIButton *startButton = [[UIButton alloc] init];
    startButton.backgroundColor = [UIColor grayColor];
    startButton.layer.cornerRadius = CORNER_RADIUS_5;
    startButton.clipsToBounds = YES;
    startButton.alpha = 0.8;
    
    // 2.设置frame
    CGSize startBtnSize = (CGSize){70,35};
    CGFloat startBtnX = ScreenWidth - startBtnSize.width - padding;
    CGFloat startBtnY = ScreenHeight - startBtnSize.height - padding;
    startButton.frame = (CGRect){startBtnX,startBtnY, startBtnSize};
    
    // 3.设置文字
    [startButton setTitle:@"跳过" forState:UIControlStateNormal];
    [startButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [startButton addTarget:self action:@selector(start) forControlEvents:UIControlEventTouchUpInside];
    [imageView addSubview:startButton];
    
}

/**
 *  开始微博
 */
- (void)start
{
    // 显示状态栏
    [UIApplication sharedApplication].statusBarHidden = NO;
    // 切换窗口的根控制器
    LoginViewController *loginVC = [[LoginViewController alloc] init];
    IWNavigationController *navigationVC = [[IWNavigationController alloc] initWithRootViewController:loginVC];
    self.view.window.rootViewController = navigationVC;
}

/**
 *  只要UIScrollView滚动了,就会调用
 */
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    // 1.取出水平方向上滚动的距离
    CGFloat offsetX = scrollView.contentOffset.x;
    
    // 2.求出页码
    double pageDouble = offsetX / scrollView.frame.size.width;
    int pageInt = (int)(pageDouble + 0.5);
    self.pageControl.currentPage = pageInt;
}

@end
